﻿using System.Buffers;
using System.Linq;
using System.Text;

using Microsoft.Extensions.DependencyInjection;


namespace Xc.DataAccess.PcpChannels
{
    /// <summary>
    /// 查询设备版本应答
    /// </summary>
    public class CloundQueryDeviceVersionRespond : RespondFrame
    {
        public CloundQueryDeviceVersionRespond(byte[] dataarea) : base(dataarea)
        {

        }

        /// <summary>
        /// 要校验的数据
        /// </summary>
        protected override MessageCode? ValidateMessageCode => MessageCode.QueryDeviceVersion;
        /// <summary>
        /// 结果
        /// </summary>
        public QueryDeviceVersion Result { get; private set; }
        /// <summary>
        /// 当前版本
        /// </summary>
        public string CurrentVersion { get; private set; }
        /// <summary>
        /// 查询设备版本
        /// </summary>
        public enum QueryDeviceVersion : byte
        {
            /// <summary>
            /// 处理成功
            /// </summary>
            OK = 0,
            /// <summary>
            /// 错误
            /// </summary>
            Error
        }

        protected override bool SetDataArea(ref System.Buffers.SequenceReader<byte> dataArea)
        {
            if (dataArea.TryRead(out var c))
            {
                Result = (QueryDeviceVersion)c;
                if (dataArea.TryRead(16, out var version))
                {
                    CurrentVersion = Encoding.ASCII.GetString(version.ToArray().Where(p=>p!=0).ToArray());
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
        public override string ToString()
        {
            return $"结果：{Result},当前版本:{CurrentVersion}";
        }
    }
}
